package factorytest.iflytek.com.einkiflytekfactorytest.engineer_test;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import factorytest.iflytek.com.einkiflytekfactorytest.common.PermissionManager;


public class BaseActivity extends AppCompatActivity {

    private Activity mActivity;
    private BaseActivityHandler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = this;

        handler = new BaseActivityHandler(this);

        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION  // hide naviagtion,and whow again when touch
//                      | View.SYSTEM_UI_FLAG_IMMERSIVE    // use with SYSTEM_UI_FLAG_HIDE_NAVIGATION.only show when wipe
                // top or bottom of screen.
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY // the same with SYSTEM_UI_FLAG_IMMERSIVE,but hide navigation
                // after a short timeout.
                // implements at frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
//                | View.SYSTEM_UI_FLAG_LOW_PROFILE;
        decorView.setSystemUiVisibility(uiOptions);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        initAllPermission();

    }

    private void initAllPermission() {

        if (isAllGotPermission()) {
            return;
        }

        //同时申请多个权限
        PermissionManager.getInstance(getApplicationContext()).execute(this,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.WRITE_SETTINGS,
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    private boolean isAllGotPermission() {
        return !((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        );
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.getInstance(getApplicationContext()).
                onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    protected Activity getActivity() {
        return mActivity;
    }

    protected Context getContext() {
        return mActivity;
    }

    @NonNull
    protected Handler getHandler() {
        return handler;
    }

    protected void handleMessage(Message msg) {

    };
}
